
<template>
<div class="doTeam_box">
  <a-row :gutter="10" class="doTeam_title" >
    <a-col :span="4">
      编程语言
    </a-col>
    <a-col :span="8">
      <a-select style="margin-top: 5px" default-value="C++" v-model="questionSubmit.language">
        <a-option v-for="languageItem in languageArr"
                  :value="languageItem.value"
                  :key="languageItem.value">
          {{ languageItem.title }}
        </a-option>
      </a-select>
    </a-col>
    <a-col :span="10" style="text-align: center">
      <a-button type="primary" @click="submitCode">
        提交
      </a-button>
    </a-col>
  </a-row>
  <div class="codeEditor">
    <CodeEditor :language="questionSubmit.language"
                :content="defaultValue"
                @editorCode="editorCode"/>
  </div>
</div>
</template>

<script setup lang="ts">
import CodeEditor from "@/components/CodeEditor.vue";
import { defineEmits, onMounted, ref } from "vue";
import { QuestionSubmitControllerService } from "../../generated";
import store from "@/store";
import { Message } from "@arco-design/web-vue";
import { useRoute } from "vue-router";

const emit = defineEmits(['submitDoTeamInfoToContest']);
const route = useRoute();
const questionSubmit = ref({
  id: 0,
  language: 'java',
  code: ''
});
const submitCode = async () => {
  // 像后端提交请求
  let res = await QuestionSubmitControllerService.addQuestionSubmitUsingPost(questionSubmit.value.code, questionSubmit.value.language, questionSubmit.value.id, store.state.user.curUser.id);
  if (res.code == 0) {
    Message.success("提交成功");
    // 通知父元素做题完成，父元素如果是在线竞赛做题，就会将提交id设置给竞赛用户信息
    emit('submitDoTeamInfoToContest', res.data.id);
  }else {
    Message.error("提交失败");
  }
}
const editorCode = (newCode:string) => {
  questionSubmit.value.code = newCode;
}
onMounted(() => {
  const id:any = route.query?.id;
  questionSubmit.value.id = id;
})
const languageArr = ref([{
  title: 'Java',
  value: 'java'
}, {
  title: 'C',
  value: 'c'
}, {
  title: 'C++',
  value: 'c++'
},{
  title: 'Python',
  value: 'python'
}, {
  title: 'JavaScript',
  value: 'javascript'
}, {
  title: 'Go',
  value: 'go'
}])
const defaultValue = ref('int main() {\nreturn 0;\n}')
</script>

<style scoped>
.doTeam_box {

}
.doTeam_title {
  line-height: 45px;
}
</style>